﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using IMDBApi.Contracts.Entities;
using IMDBApi.Contracts.Entities.Search;
using IMDBApi.Contracts.Entities.Title.MainDetail;

namespace IMDBApi.Contracts
{
    public class Query : Interface
    {
        /// <summary>
        /// ...
        /// </summary>
        private static List<string> blackList = new List<string>() { "RATING", "GERMAN", "AC3", "BDRIP", "XVID" };

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="searchTerm"></param>
        /// <returns></returns>
        public IMovie FindMovie(string searchTerm)
        {
            string json = this.FindMovieJson(searchTerm);
            MainDetail mainDetail = JSONSerializer.Deserialize<MainDetail>(json);

            return new Movie() { Id = mainDetail.Data.TConst, Name = mainDetail.Data.Title, Rating = mainDetail.Data.Rating };
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="searchTerm"></param>
        /// <returns></returns>
        public IActor FindActor(string searchTerm)
        {
            return new Actor();
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="searchTerm"></param>
        /// <returns>Returns JSON formatted string.</returns>
        public string FindMovieJson(string searchTerm)
        {
            string json = new IMDBSearch().GetSearchResults(CreateSearchTerm(searchTerm));
            Search results = JSONSerializer.Deserialize<Search>(json);

            if (results.Data.Results == null)
                return string.Empty;

            json = new IMDBMovie(string.Format("http://www.imdb.com/title/{0}/", results.Data.Results[0].List[0].Tconst)).GetMainDetails();
            return json;
        }

        /// <summary>
        /// ...
        /// </summary>
        /// <param name="searchTerm"></param>
        /// <returns>Returns JSON formatted string.</returns>
        public string FindActorJson(string searchTerm)
        {
            return string.Empty;
        }

        /// <summary>
        /// Creates the term for the web query. Filters out any word in blacklist.
        /// </summary>
        /// <param name="filename">A filename.</param>
        /// <returns>Returns a query term.</returns>
        public string CreateSearchTerm(string filename)
        {
            MatchCollection matches = Regex.Matches(filename, @"(?<part>(.*?))\.", RegexOptions.IgnoreCase);

            var searchTerms = string.Join(" ", from m in matches.OfType<Match>()
                                                    where !blackList.Any( w => m.Groups["part"].Value.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0)
                                                        select m.Groups["part"].Value);

            return searchTerms;
        }
    }
}
